package ugeOpgaver;

public class Uge6 {

	public static void main(String[] args) {
		Uge6Player player1 = new Uge6Player("Jon");
		Uge6Player player2 = new Uge6Player("Mikkel");
		player1.setOpponent(player2);
		player2.setOpponent(player1);
		
		boolean winnerFound = false;
		
		Thread p1Thread = new Thread(player1);
		Thread p2Thread = new Thread(player2);
		p1Thread.start();
		p2Thread.start();
		
		game(6, player1, player2);
		
		do {
			System.out.println("");
			System.out.println("****** Stillingen er: ******");
			System.out.println(player1.getName() + " : " + player1.getScore() + " point.");
			System.out.println(player2.getName() + " : " + player2.getScore() + " point.");
			System.out.println("");
			
			if (player1.getScore() > player2.getScore()){
				System.out.println(player1.getName() + " vinder.");
				winnerFound = true;
			} else if (player1.getScore() < player2.getScore()){
				System.out.println(player2.getName() + " vinder.");
				winnerFound = true;
			} else if (player1.getScore() == player2.getScore()){
				System.out.println("Stillingen er uafgjort - der spilles en bold mere!");
				game(1, player1, player2);
			}			
		} while(!winnerFound);
		player1.setGame(false);
		player2.setGame(false);
	}
	
	public static void game(int rounds, Uge6Player player1, Uge6Player player2){
		int i=0;
		int max=rounds;
		do{		
			if (player1.hasBall()==true || player2.hasBall() == true){
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} else if (i<max) {
				i++;
				System.out.println("");
				System.out.println("Sæt " + i + " af " + max);
				player1.setHasBall(true);
			} else {
				i++;
			}
		} while(i<=max);
	}
}
